package com.ai.javaailangchain4j.config;

import com.ai.javaailangchain4j.store.MongoChatMemoryStore;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.DocumentSplitter;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.splitter.DocumentByParagraphSplitter;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.memory.chat.ChatMemoryProvider;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.List;

@Configuration
public class XiaozhiAgentConfig {

    @Autowired
    private MongoChatMemoryStore mongoChatMemoryStore;

    @Autowired
    private EmbeddingModel embeddingModel;
    @Autowired
    private EmbeddingStore<TextSegment> embeddingStore;

    @Bean
    public ChatMemoryProvider chatMemoryProviderXiaozhi() {
        return memoryId -> MessageWindowChatMemory.builder()
                .id(memoryId)
                .maxMessages(20)
                .chatMemoryStore(mongoChatMemoryStore)
                .build();
    }

    @Bean
    public ContentRetriever contentRetrieverXiaozhi() {
//        文档解析
        Document document1 = FileSystemDocumentLoader.loadDocument("D:\\java_ai\\knowledge\\医院信息.md");
        Document document2 = FileSystemDocumentLoader.loadDocument("D:\\java_ai\\knowledge\\科室信息.md");
        Document document3 = FileSystemDocumentLoader.loadDocument("D:\\java_ai\\knowledge\\神经内科.md");

        List<Document> documents = Arrays.asList(document1, document2, document3);
//        使用内存向量检索
        InMemoryEmbeddingStore<TextSegment> embeddingStore = new InMemoryEmbeddingStore<>();

//        创建了一个文档分割器，用于将文档按段落分割成大小为1000个字符的文本块，便于后续向量化存储和检索。
        DocumentSplitter documentSplitter = new DocumentByParagraphSplitter(1000, 0);

        EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
                .documentSplitter(documentSplitter)
                .embeddingStore(embeddingStore)
                .build();

        ingestor.ingest(documents);

//        从嵌入存储里（embeddingStore）中检索内容
        return EmbeddingStoreContentRetriever.from(embeddingStore);
    }

    @Bean
    ContentRetriever contentRetrieverPinecone() {
        // 创建一个 EmbeddingStoreContentRetriever 对象，用于从嵌入存储中检索内容

        return EmbeddingStoreContentRetriever.builder()
                .embeddingModel(embeddingModel)//设置嵌入模型
                .embeddingStore(embeddingStore)//设置嵌入存储
                .maxResults(1)//设置最大结果数
                .minScore(0.8).build();//设置最小分数
    }
}
